package com.xtaifghy.wugui;

import java.util.Random;
import java.util.ArrayList;

public class GameState1 implements GameStateInterface {
    private boolean [] state = {true,true,true,true,true,true,true,true};
    private boolean [] goalState = {false,true,false,false,false,false,false,false};
    private static Random random = new Random();
    public static final int[][] actions={
            {0,1,3},//0
            {0,1,2},//1
            {1,2,4},//2
            {0,5,3},//3
            {1,4,6,3},//4
            {2,4,7},//5
            {6,5,3},//6
            {6,5,7},//7
            {6,4,7},//8
    };

    public GameState1() {
        //randomGoalState();
    }

    @Override
    public ArrayList<boolean[]> getPaths() {
        GameState1 tempGame = new GameState1();
        tempGame.setState(getState());
        tempGame.setGoalState(getGoalState());
        boolean [] path = {false,false,false,false,false,false,false,false,false};
        while(!tempGame.testState()){
            int a = random.nextInt(9);
            path[a] = !path[a];
            tempGame.doAction(a);
        }
        ArrayList<boolean[]> paths = new ArrayList<>();
        paths.add(path);
        return paths;
    }

    @Override
    public void randomGoalState() {
        for (int i = 0; i < goalState.length; i++) {
            goalState[i] = random.nextBoolean();
        }
    }

    @Override
    public void doAction(int a){
        if(a<0||a>actions.length)return;
        for (int i:actions[a]) {
            state[i]=!state[i];
        }
    }

    @Override
    public boolean testState() {
        for (int i = 0; i < state.length; i++) {
            if(state[i]!=goalState[i]) return false;
        }
        return true;
    }

    @Override
    public void setState(boolean[] state) {
        this.state = state.clone();
    }

    @Override
    public boolean[] getState() {
        return state.clone();
    }

    @Override
    public void setGoalState(boolean[] goalState) {
        this.goalState = goalState.clone();
    }

    @Override
    public boolean[] getGoalState() {
        return goalState.clone();
    }
}

